// 加载仪表盘数据
async function loadDashboardData() {
    try {
        const response = await fetch(`${API_BASE_URL}/dashboard`, {
            method: 'GET',
            credentials: 'same-origin'
        });
        
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        
        const data = await response.json();
        updateDashboardStats(data.stats);
        displayRecentOrders(data.recentOrders);
        displayDeliveryPersons(data.deliveryPersons);
    } catch (error) {
        console.error('加载仪表盘数据失败:', error);
        showAlert('加载仪表盘数据失败，请刷新页面重试。', 'danger');
    }
}

// 更新仪表盘统计数据
function updateDashboardStats(stats) {
    if (!stats) return;
    
    userCountElement.textContent = stats.userCount || 0;
    deliveryPersonCountElement.textContent = stats.deliveryPersonCount || 0;
    orderCountElement.textContent = stats.orderCount || 0;
    completedOrderCountElement.textContent = stats.completedOrderCount || 0;
}

// 显示最近订单
function displayRecentOrders(orders) {
    if (!orders || orders.length === 0) {
        recentOrdersTable.innerHTML = '<tr><td colspan="5" class="text-center">暂无订单数据</td></tr>';
        return;
    }
    
    const ordersHtml = orders.map(order => `
        <tr>
            <td>${order.orderNumber}</td>
            <td>${order.shipperInfo ? order.shipperInfo.name : '未知'}</td>
            <td>${order.recipientInfo ? order.recipientInfo.name : '未知'}</td>
            <td><span class="badge ${getOrderStatusBadgeClass(order.status)}">${formatOrderStatus(order.status)}</span></td>
            <td>
                <button class="btn btn-sm btn-outline-primary" onclick="viewOrderDetail('${order.orderNumber}')">查看</button>
            </td>
        </tr>
    `).join('');
    
    recentOrdersTable.innerHTML = ordersHtml;
}

// 显示配送员
function displayDeliveryPersons(deliveryPersons) {
    if (!deliveryPersons || deliveryPersons.length === 0) {
        deliveryPersonsTable.innerHTML = '<tr><td colspan="4" class="text-center">暂无配送员数据</td></tr>';
        return;
    }
    
    const deliveryPersonsHtml = deliveryPersons.map(person => `
        <tr>
            <td>${person.name}</td>
            <td>${person.phone}</td>
            <td><span class="badge ${getDeliveryPersonStatusBadgeClass(person.status)}">${formatDeliveryPersonStatus(person.status)}</span></td>
            <td>
                <button class="btn btn-sm btn-outline-primary" onclick="viewDeliveryPersonDetail('${person.id}')">查看</button>
            </td>
        </tr>
    `).join('');
    
    deliveryPersonsTable.innerHTML = deliveryPersonsHtml;
}

// 查看订单详情
function viewOrderDetail(orderNumber) {
    console.log('查看订单详情:', orderNumber);
    // 实现查看订单详情的功能
    showAlert(`订单详情功能正在开发中 (订单号: ${orderNumber})`, 'info');
}

// 查看配送员详情
function viewDeliveryPersonDetail(id) {
    console.log('查看配送员详情:', id);
    // 实现查看配送员详情的功能
    showAlert(`配送员详情功能正在开发中 (ID: ${id})`, 'info');
}

// 导出函数
window.loadDashboardData = loadDashboardData;
window.viewOrderDetail = viewOrderDetail;
window.viewDeliveryPersonDetail = viewDeliveryPersonDetail;